package com.ctshk.rpc.order.season.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.service.ISupplierOrderService;
import com.ctshk.rpc.iotc.service.IOperateBankPreferenceService;
import com.ctshk.rpc.order.goods.service.IOrderService;
import com.ctshk.rpc.order.season.dto.SeasonOrderPayDTO;
import com.ctshk.rpc.order.season.dto.SeasonOrderTouristDataDTO;
import com.ctshk.rpc.order.season.dto.SeasonProductOrderCommodityDTO;
import com.ctshk.rpc.order.season.entity.SeasonOrder;
import com.ctshk.rpc.order.season.entity.SeasonProductOrderCommodity;
import com.ctshk.rpc.order.season.mapper.SeasonOrderMapper;
import com.ctshk.rpc.order.season.mapper.SeasonProductOrderCommodityMapper;
import com.ctshk.rpc.order.season.service.ISeasonOrderTouristService;
import com.ctshk.rpc.order.season.service.ISeasonProductOrderCommodityService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 套票-订单-当地玩乐订单 服务实现类
 * </p>
 *
 * @author 杨林
 * @since 2021-03-24
 */
@DubboService
public class SeasonProductOrderCommodityServiceImpl extends ServiceImpl<SeasonProductOrderCommodityMapper, SeasonProductOrderCommodity> implements ISeasonProductOrderCommodityService {

    @Autowired
    private SeasonProductOrderCommodityMapper orderCommodityMapper;

    @Autowired
    private SeasonOrderMapper seasonOrderMapper;

    @Autowired
    private ISeasonOrderTouristService seasonOrderTouristService;

    @DubboReference
    private ISupplierOrderService supplierOrderService;

    @DubboReference
    private ISysUserService iSysUserService;
    @DubboReference
    private IOrderService orderService;


    @DubboReference
    private IOperateBankPreferenceService iOperateBankPreferenceService;


    /* @MethodName: 保存本地玩乐 订单信息
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/25 9:42
     **/
    @Override
    public void saveCommodityProduct(List<SeasonProductOrderCommodityDTO> CommodityList, Long userId, Long orderId) {
        LocalDateTime now = LocalDateTime.now();
        List<SeasonProductOrderCommodity> list = CommodityList.stream().map(
                CommodityReq -> {
                    SeasonProductOrderCommodity Commodity = EntityUtil.copy(CommodityReq, SeasonProductOrderCommodity.class);
                    Commodity.setCreateId(userId);
                    Commodity.setGmtCreate(now);
                    Commodity.setIsDeleted(IsDeletedCode.NO.getCode());
                    Commodity.setSeasonOrderId(orderId);
                    Commodity.setId(SnowflakeIdWorker.nextId());
                    return Commodity;
                }
        ).collect(Collectors.toList());
        orderCommodityMapper.insertBatchSomeColumn(list);
    }

    /* @MethodName: 获取本地玩乐订单信息
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/30 16:52
     **/
    @Override
    public List<SeasonProductOrderCommodityDTO> getOrderCommodityList(Long id) {
        List<SeasonProductOrderCommodityDTO> resultList = EntityUtil.copyList(
                orderCommodityMapper.selectList(
                        new QueryWrapper<SeasonProductOrderCommodity>().lambda()
                                .eq(SeasonProductOrderCommodity::getSeasonOrderId, id)
                                .eq(SeasonProductOrderCommodity::getIsDeleted, IsDeletedCode.NO.getCode())

                ), SeasonProductOrderCommodityDTO.class);

        return resultList;
    }


    @Override
    public void playSupplierOrder(Long orderId, TokenUser tokenUser) {

    }


    /* @MethodName: 本地玩乐 释放库存
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/13 17:59
     **/
    @Override
    public void backStock(Long orderId, Long userId) {

    }


    /* @MethodName: 动态套票 下当地玩乐 单项单 生成对应的订单号
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/13 19:04
     **/
    @Override
    public void playCommodityOrder(Long orderId, TokenUser user, List<SeasonOrderPayDTO> payList, List<SeasonOrderTouristDataDTO> touristList) {

    }


    /* @MethodName: 本地玩乐 下供应商 预订单
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/12 15:23
     **/
    private void playSupplierCommodityOrder(SeasonOrder seasonOrder, List<SeasonOrderTouristDataDTO> touristList, List<SeasonProductOrderCommodityDTO> CommodityList, Long supplierId, String supplierName, UserDTO user) {

    }

    /* @MethodName: 动态套票 下各自订单后 确认对应状态
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/14 10:57
     **/
    @Override
    public void changeAffirmState(Long orderId, Integer isAffirm) {
        UpdateWrapper<SeasonProductOrderCommodity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SeasonProductOrderCommodity::getOrderId, orderId);
        updateWrapper.lambda().set(SeasonProductOrderCommodity::getScheduledStatus, isAffirm);
        orderCommodityMapper.update(null, updateWrapper);
    }

    /* @MethodName: 退款状态
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/20 10:25
     **/
    @Override
    public void changeCancelOrderState(Long orderId, Integer cancelStatus) {
        UpdateWrapper<SeasonProductOrderCommodity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SeasonProductOrderCommodity::getOrderId, orderId);
        updateWrapper.lambda().set(SeasonProductOrderCommodity::getCancelStatus, cancelStatus);
        orderCommodityMapper.update(null, updateWrapper);
    }

    /* @MethodName: 下本地玩乐的 订单  以内部挂账形式
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/20 17:54
     **/
    @Override
    public void playInternalOrder(Long orderId, TokenUser user, List<SeasonOrderTouristDataDTO> touristList) {

    }


    /* @MethodName: 获取对应的价格（动态套票订单 单项产品的价格）
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/20 18:07
     **/
    public BigDecimal getParallelismPrice(BigDecimal seasonAmount, BigDecimal singleAmount, BigDecimal payAmount) {
        return null;
    }


    public SeasonOrder getSeasonOrderById(Long id) {
        return seasonOrderMapper.selectById(id);
    }
}
